<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/5
 * Time: 17:19
 */
defined('IN_TS') or die('Access Denied.');


$code = trim($_GET['code']);
$upline = $_GET['upline'];

if($_SESSION['backurl']==''){
    $url=SITE_URL;
}else{
    $url=$_SESSION['backurl'];
}

header("Location:".$url);
#print_r($strGroup);

$arrAo = getWeixinLoginAccesstokenOpenId('wx2ef8a11062078826','16863a6f1c9e1a438b5c05922681c3d3',$code);

#print_r($arrAo);exit;


if($arrAo['access_token'] && $arrAo['openid']){



    $openid = $arrAo['openid'];
    $unionid =  $arrAo['unionid'];

    $access_token = $arrAo['access_token'];



    $strOpen = $new['user']->find('user_open',array(
        'sitename'=>'weixin',
        'openid'=>$unionid,
    ));

    //10天更换一次access_token
    if(time()-10*86400>$strOpen['uptime']){
        $new['user']->update('user_open',array(
            'sitename'=>'weixin',
            'openid'=>$unionid,
        ),array(
            'access_token'=>$access_token,
            'uptime'=>time(),
        ));
    }


    if($strOpen['userid']){

        $userInfo = $new['user']->find('user_info',array(
            'userid'=>$strOpen['userid']
        ));
        if($userInfo){

            //更新登录时间
            $new['user']->update('user_info',array(
                'userid'=>$strOpen['userid'],
            ),array(
                'ip'=>getIp(),  //更新登录ip
                'uptime'=>time(),   //更新登录时间
                'wxopenid'=>$openid,
            ));
            $new['user']->update('group_user',array(
                'userid'=>$strOpen['userid'],
            ),array(
                'uptime'=>time(),   //更新登录时间
            ));

            $userData = $new['user']->find('user_info',array(
                'userid'=>$strOpen['userid'],
            ));

            $_SESSION['tsuser']	= $userData;
            $GLOBALS['TS_USER'] = $userData;

            $_SESSION['expiretime'] = time() + 3600; // 刷新时间戳

        }else{
            $new['user']->delete('user',array('userid'=>$strOpen['userid']));
            $new['user']->delete('user_open',array('userid'=>$strOpen['userid']));
            $new['user']->delete('user_follow',array('userid'=>$strOpen['userid']));
        }

       exit;

    }else{

        //获取用户基本资料
        $arrUserInfo = getWeixinUserInfo($arrAo['access_token'] , $arrAo['openid']);

        switch($arrUserInfo['sex']){
            case 0:
                $sex='保密';
                break;
            case 1:
                $sex='男';
                break;
            case 2:
                $sex='女';
                break;
        }

        $salt = md5(rand());

        $pwd = random(5,0);

        $userid = $new['user']->create('user',array(
            'pwd'=>md5($salt.$pwd),
            'salt'=>$salt,
            'email'=>$unionid,
        ));

        $arrUser = array(
            'userid'			=> $userid,
            'username' 	=> $arrUserInfo['nickname'],
            'email'		=> $unionid,
            'wxopenid' => $openid,
            'sex'=> $sex,
            'ip'			=> getIp(),
            'addtime'	=> time(),
            'uptime'	=> time(),
        );
        if($upline != ''){
            $arrUser['follow_uid'] = $upline;
        };

        //插入ts_user_info
        $new['user']->create('user_info',$arrUser);

        //插入ts_user_open
        $new['user']->create('user_open',array(
            'userid'=>$userid,
            'sitename'=>'weixin',
            'openid' => $unionid,
            'access_token'=>$access_token,
            'uptime'=>time(),
        ));


        if($upline!=''){
            $followUser = $new['user']->find('user_info',array(
                'userid'=>$upline,
            ));
            $new['user']->update('user_info',array(
                'userid'=>$upline,
            ),array(
                'count_followed'=>$followUser['count_followed']+1,
            ));
            $new['user']->update('user_info',array(
                'userid'=>$userid,
            ),array(
                'follow_uid'=>$upline,
            ));
            $new['user']->create('user_follow',array(
                'userid'=>$userid,
                'userid_follow'=>$upline,
                'addtime'=>time(),
            ));
            //对积分进行处理
            aac('user') -> doScore('user', 'follow', 'do',$upline);
        }



        //更新用户头像
        if($arrUserInfo['headimgurl']!='' && $arrUserInfo['headimgurl']!='/0'){
            //1000个图片一个目录
            $menu2=intval($userid/1000);
            $menu1=intval($menu2/1000);
            $menu = $menu1.'/'.$menu2;
            $photo = $userid.'.jpg';

            $photos = $menu.'/'.$photo;

            $new['user']->update('user_info',array(
                'userid'=>$userid,
            ),array(
                'path'=>$menu,
                'face'=>$photos,
            ));

            $dir = 'uploadfile/user/' . $menu;

            $dfile = $dir . '/' . $photo;


            createFolders($dir);

            if (!is_file($dfile)) {
                $img = file_get_contents($arrUserInfo['headimgurl']);
                file_put_contents($dfile, $img);
            };

        }


        //获取用户信息
        $userData = $new['user']->find('user_info',array(
            'userid'=>$userid,
        ));
/*
        $arrDuty =$new['user']->findAll('duty','userid='.$userid.' and status>-1','addtime desc',null,'0,30');
        $userData['dutyid'] = $arrDuty[0]['dutyid'];
*/
        $_SESSION['tsuser']	= $userData;

        $_SESSION['expiretime'] = time() + 3600; // 刷新时间戳

        exit;

    }




}